package com.wzh.himalaya.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;


/**
 * className:RoundRectImageView
 * Author:WZH
 * Time:2022-02-22
 */
public class RoundRectImageView extends AppCompatImageView {

    private float roundRatio = 0.1f;
    private Path path;

    public RoundRectImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(path ==null) {
           path = new Path();
            path.addRoundRect(new RectF(0,0,getWidth(),getHeight()),roundRatio*getWidth(),roundRatio*getHeight(),Path.Direction.CW);
        }
       canvas.save();
        canvas.clipPath(path);
        super.onDraw(canvas);
        canvas.restore();
    }
}
